//
//  RBStockAddOrderVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/17.
//

#import "RBStockAddOrderVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "UIButton+RBAdd.h"
#import "RBStockAddOrder2VC.h"
#import "RBGoodsDetailMultipleChoosePopVC.h"
#import "RBGoodsDetailParamPopVC.h"
#import "RBGoodsAddToothPopVC.h"

@interface RBStockAddOrderVC ()

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *toothArray;
@property (nonatomic, strong) NSMutableArray *toothBtnsArrayM;

@property (nonatomic, strong) RBStockAddOrderResponse *response;
@property (nonatomic, retain) NSArray *specArray;

@end

@implementation RBStockAddOrderVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"填写产品信息";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.scrollView view_clear];
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    //商品信息
    [self.scrollView addView:[self goodsView] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    //属性
    self.specArray = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        for (RBAddOrderSpecModel *model in self.response.data.goods_spec) {
            if (model.status == 1) {
                model.level = 2;
                if (model.spec.count) {
                    RBAddOrderSpecItemModel *model2 = model.spec.firstObject;
                    if (model2.spec_son.count) {
                        model.level = 3;
                    }
                }
                
                if (model.model_type == 1) {
                    for (RBAddOrderSpecItemModel *itemModel in model.spec) {
                        if (itemModel.is_default) {
                            model.selIndex = [model.spec indexOfObject:itemModel];
                            break;
                        }
                    }
                }
                [arrayM addObject:model];
            }
        }
        arrayM;
    });
    //线
    [self.scrollView addView:[UIView new] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(12) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    //
    for (RBAddOrderSpecModel *model in self.specArray) {
        /**模板类型: 1单选模板[显示]; 2多选模板[弹出]; 3选择器模板[弹出];
         4单选模板[弹出][多项]; 5牙位模板[显示]; 6牙排模板[弹出]    */
        if (model.model_type == 1) { //单选
            [self.scrollView addView:[self paramsSingleDisplayType1ViewWithData:model] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        } else if (model.model_type == 2) { //多选
            [self.scrollView addView:[self paramsMultiplePopType2ViewWithData:model] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        } else if (model.model_type == 3) { //选择器模板
            [self.scrollView addView:[self paramsSelectorPopType3ViewWithData:model] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        } else if (model.model_type == 4) {
            [self.scrollView addView:[self paramsSinglePopType4ViewWithData:model] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        } else if (model.model_type == 5) {
            [self.scrollView addView:[self paramsChooseToothDisplayType5ViewWithData:model] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        } else if (model.model_type == 6) {
            [self.scrollView addView:[self paramsAddToothPopType6ViewWithData:model] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        }
    }
}

#pragma mark - Network

- (void)getData {  
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"good_id"] = self.submitModel.goods_id;
    } path:_Goods_detail post:YES log:YES responseClass:RBStockAddOrderResponse.class desView:self.view showHud:NO showPlaceHolder:NO sucBlock:^(id data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBStockAddOrderResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:^{
        [weakSelf getData];
    }];
}

#pragma mark - Event
//单选显示
- (void)paramsType1ItemTapped:(UITapGestureRecognizer *)ges
{
    UIView *view = ges.view.superview;
    RBAddOrderSpecModel *model = view.rbExData;
    model.selIndex = ges.view.tag - 1000;
    for (UIView *itemView in view.subviews)
    {
        if(itemView.tag >= 1000)
        {
            UIImageView *imgView = (UIImageView *)[itemView viewWithTag:333];
            imgView.image = [UIImage imageNamed:model.selIndex == itemView.tag - 1000? @"addressListDefault": @"goodsDetailParamsUnSel"];
        }
    }
}
//多选弹窗
- (void)paramsType2ViewTapped: (UITapGestureRecognizer *)ges
{
    __weak typeof(self) weak_self = self;
    RBAddOrderSpecModel *model = ges.view.rbExData;
    RBGoodsDetailMultipleChoosePopVC *vc = RBGoodsDetailMultipleChoosePopVC.new;
    vc.title = model.name;
    vc.options = model.spec;
    vc.dismissBlock = ^{
        if (ges.view.tag == 2002)
        {
            [weak_self reloadParamsType2:ges.view];
        }
    };
    [self presentVc:vc];
}

- (void)reloadParamsType2: (UIView *)view
{
    RBAddOrderSpecModel *model = view.rbExData;
    UILabel *desL = (UILabel *)[view viewWithTag:333];
    desL.text = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        for (RBAddOrderSpecItemModel *item in model.spec)
        {
            if(item.selected)
            {
                [arrayM addObject:item.spec_name];
            }
        }
        [arrayM componentsJoinedByString:@" "];
    });
}
//多选
- (void)paramsType3ViewTapped: (UITapGestureRecognizer *)ges
{
    __weak typeof(self) weak_self = self;
    RBAddOrderSpecModel *model = ges.view.rbExData;
    RBGoodsDetailParamPopVC *vc = RBGoodsDetailParamPopVC.new;
    vc.title = model.name;
    vc.options = model.spec;
    vc.level = model.level;
    vc.dismissBlock = ^{
        if (ges.view.tag == 2003)
        {
            [weak_self reloadParamsType3:ges.view];
        }
    };
    [self presentVc:vc];
}

- (void)reloadParamsType3: (UIView *)view
{
    RBAddOrderSpecModel *model = view.rbExData;
    UILabel *desL = (UILabel *)[view viewWithTag:333];
    desL.text = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        if(model.level == 2)
        {
            for (RBAddOrderSpecItemModel *item in model.spec)
            {
                if(item.selected)
                {
                    [arrayM addObject:item.spec_name];
                    break;
                }
            }
        }
        else if(model.level == 3)
        {
            for (RBAddOrderSpecItemModel *gModel in model.spec)
            {
                for (RBAddOrderSpecItemModel *item in gModel.spec_son)
                {
                    if(item.selected)
                    {
                        [arrayM addObject:gModel.spec_name];
                        [arrayM addObject:item.spec_name];
                        break;
                    }
                }
            }
        }
        [arrayM componentsJoinedByString:@" "];
    });
}

- (void)paramsType4ViewTapped: (UITapGestureRecognizer *)ges
{
    __weak typeof(self) weak_self = self;
    RBAddOrderSpecModel *model = ges.view.rbExData;
    RBGoodsDetailMultipleChoosePopVC *vc = RBGoodsDetailMultipleChoosePopVC.new;
    vc.title = model.name;
    vc.options = model.spec;
    vc.singleSelect = YES;
    vc.dismissBlock = ^{
        if (ges.view.tag == 2004) {
            [weak_self reloadParamsType4:ges.view];
        }
    };
    [self presentVc:vc];
}

- (void)reloadParamsType4: (UIView *)view
{
    RBAddOrderSpecModel *model = view.rbExData;
    UILabel *desL = (UILabel *)[view viewWithTag:333];
    desL.text = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        for (RBAddOrderSpecItemModel *item in model.spec)
        {
            if(item.selected)
            {
                [arrayM addObject:item.spec_name];
            }
        }
        [arrayM componentsJoinedByString:@" "];
    });
}

- (void)paramsType6ViewTapped: (UITapGestureRecognizer *)ges
{
    __weak typeof(self) weak_self = self;
    RBAddOrderSpecModel *model = ges.view.rbExData;
    RBGoodsAddToothPopVC *vc = RBGoodsAddToothPopVC.new;
    vc.title = model.name;
    vc.options = model.spec;
    vc.dismissBlock = ^{
        if (ges.view.tag == 2006) {
            [weak_self reloadParamsType6:ges.view];
        }
    };
    [self presentVc:vc];
}

- (void)reloadParamsType6: (UIView *)view
{
    RBAddOrderSpecModel *model = view.rbExData;
    UILabel *desL = (UILabel *)[view viewWithTag:333];
    desL.text = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        for (RBAddOrderSpecItemModel *item in model.spec)
        {
            if(item.selected)
            {
                [arrayM addObject:[NSString stringWithFormat:@"%@(x%zd)",item.spec_name,item.count]];
            }
        }
        [arrayM componentsJoinedByString:@" "];
    });
}


- (void)toothBtnTapped: (UIButton *)btn
{
    btn.selected = !btn.selected;
    
    NSInteger tag = btn.tag;
    NSInteger section = tag / 20;
    NSInteger row = tag % 20;
    
    for (RBAddOrderSpecModel *model in self.specArray) {
        if (model.model_type == 5) {
            RBAddOrderSpecItemModel *model2 = model.spec[section];
            RBAddOrderSpecItemModel *item = model2.spec_son[row];
            item.selected = btn.selected;
        }
    }
}

- (void)nextBtnTapped
{
    for (RBAddOrderSpecModel *model in self.specArray) {
        NSString *message = [NSString stringWithFormat:@"请选择%@", model.name];
        NSInteger count = 0;
        if (model.model_type == 1) {
            count = 6;
        } else if (model.model_type == 6) {
            for (RBAddOrderSpecItemModel *item in model.spec) {
                if (item.selected) {
                    count ++;
                }
            }
        } else {
            if (model.level == 2) {
                for (RBAddOrderSpecItemModel *item in model.spec) {
                    if (item.selected) {
                        count ++;
                    }
                }
            } else if (model.level == 3) {
                for (RBAddOrderSpecItemModel *gModel in model.spec) {
                    for (RBAddOrderSpecItemModel *item in gModel.spec_son) {
                        if (item.selected) {
                            count ++;
                        }
                    }
                }
            }
        }
        if (count == 0) {
            [self.view showTip:message completion:nil];
            return;
        }
    }
    NSMutableArray *chooseSpecArray = [NSMutableArray array];
    for (RBAddOrderSpecModel *model in self.specArray) {
        NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
        dicM[@"id"] = model.id;
        
        NSMutableArray *arrayM = NSMutableArray.array;
        dicM[@"options"] = arrayM;
        
        if (model.model_type == 1) {
            RBAddOrderSpecItemModel *item = model.spec[model.selIndex];
            [arrayM addObject:({
                NSMutableDictionary *itemM = NSMutableDictionary.dictionary;
                itemM[@"sid"] = item.id;
                itemM[@"pid"] = @"0";
                itemM[@"num"] = @"0";
                itemM[@"price"] = item.spec_price;
                itemM;
            })];
        } else if (model.model_type == 2 || model.model_type == 4 || model.model_type == 6 || (model.model_type == 3 && model.level == 2)) {
            for (RBAddOrderSpecItemModel *item in model.spec) {
                if (item.selected) {
                    [arrayM addObject:({
                        NSMutableDictionary *itemM = NSMutableDictionary.dictionary;
                        itemM[@"sid"] = item.id;
                        itemM[@"pid"] = @"0";
                        
                        NSString *numString = @"0";
                        if (model.model_type == 6) {
                            numString = [NSString stringWithFormat:@"%zd", item.count];
                        }
                        itemM[@"num"] = numString;
                        itemM[@"price"] = item.spec_price;
                        itemM;
                    })];
                }
            }
        } else if (model.model_type == 5 || (model.model_type == 3 && model.level == 3)) {
            for (RBAddOrderSpecItemModel *gModel in model.spec) {
                for (RBAddOrderSpecItemModel *item in gModel.spec_son) {
                    if (item.selected) {
                        [arrayM addObject:({
                            NSMutableDictionary *itemM = NSMutableDictionary.dictionary;
                            itemM[@"sid"] = item.id;
                            itemM[@"pid"] = item.pid;
                            itemM[@"num"] = @"0";
                            itemM[@"price"] = item.spec_price;
                            itemM;
                        })];
                    }
                }
            }
        }
        [chooseSpecArray addObject:dicM];
    }
    
    NSString *specString = chooseSpecArray.jsonStringEncoded;
    
    self.submitModel.spec_key = specString;
    self.submitModel.order_type = @"6";
    self.submitModel.mail_type = @"1";
    self.submitModel.user_id = [RBAccountManager getUserId];
    
    RBStockAddOrder2VC *vc = RBStockAddOrder2VC.new;
    vc.allModel = self.response.data;
    vc.submitModel = self.submitModel;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)bottomView
{
    if (!_bottomView)
    {
        self.bottomView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(60) + kRBSafeBottomHeight);
            }];
            
            UIStackView *stackView = UIStackView.new;
            stackView.spacing = Zoom(20);
            stackView.axis = UILayoutConstraintAxisHorizontal;
            stackView.distribution = UIStackViewDistributionFillEqually;
            [view addSubview:stackView];
            [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(Zoom(15));
                make.right.mas_equalTo(-Zoom(15));
                make.top.mas_equalTo(Zoom(10));
                make.height.mas_equalTo(Zoom(40));
            }];
            [stackView addArrangedSubview:({
                UILabel *view = [self btnWithTitle:@"下一步" type:1];
                [view rbAddTapGesWithTarget:self action:@selector(nextBtnTapped)];
                view;
            })];
            view;
        });
    }
    return _bottomView;
}

- (UILabel *)btnWithTitle: (NSString *)title type: (NSInteger)type
{
    if (type == 0)
    {
        UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textLightBlackColor] align:1 text:title bgColor:UIColor.whiteColor radius:Zoom(20)];
        [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig textGrayColor]];
        return view;
    }
    else
    {
        return [RanTool labelWithFontSize:Zoom(13) weight:1 color:UIColor.whiteColor align:1 text:title bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(20)];
    }
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.bottom.mas_equalTo(self.bottomView.mas_top);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)goodsView
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    [view rbSetCornerRadius:Zoom(8)];
    
    [view h_view_addView:[RanTool imgViewWithMode:2 radius:0 imgName:self.response.data.picurl] withHeight:Zoom(99) width:Zoom(99) top:Zoom(12) bottom:Zoom(12) left:Zoom(12) right:0 centerY:0];
    
    [view h_view_addView:({
        UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:self.response.data.good_name];
        view.numberOfLines = 3;
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(25) bottom:CGFLOAT_MIN left:Zoom(8) right:Zoom(12) centerY:0];
    
    return view;
}

/** 1单选模板[显示] */
- (UIView *)paramsSingleDisplayType1ViewWithData: (RBAddOrderSpecModel *)data
{
    UIView *view = UIView.new;
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:data.name];
                [view addPriority:1];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"(必选)"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            view;
        }) withHeight:Zoom(44) width:CGFLOAT_MIN top:Zoom(4) bottom:Zoom(10) left:Zoom(16) right:Zoom(16) centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            view.rbExData = data;
            for (NSInteger i = 0; i < data.spec.count; i++)
            {
                RBAddOrderSpecItemModel *model = data.spec[i];
                [view h_view_addView:({
                    UIView *view = UIView.new;
                    [view rbAddTapGesWithTarget:self action:@selector(paramsType1ItemTapped:)];
                    view.tag = 1000 + i;
                    [view h_view_addView:({
                        UIImageView *view = [RanTool imgViewWithMode:0 radius:0 imgName:data.selIndex == i? @"addressListDefault": @"goodsDetailParamsUnSel"];
                        view.tag = 333;
                        view;
                    }) withHeight:Zoom(12) width:Zoom(12) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:0 right:0 centerY:YES];
                    [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:model.spec_name] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(7) right:0 centerY:0];
                    view;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:i?Zoom(16):0 right:CGFLOAT_MIN centerY:0];
            }
            view;
        }) withHeight:Zoom(44) width:CGFLOAT_MIN top:0 bottom:Zoom(12) left:Zoom(16) right:Zoom(16) centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:Zoom(12) left:Zoom(15) right:Zoom(15) centerX:NO];
    return view;
}
/** 2多选模板[弹出] */
- (UIView *)paramsMultiplePopType2ViewWithData: (RBAddOrderSpecModel *)data
{
    UIView *view = UIView.new;
    view.tag = 2002;
    view.rbExData = data;
    [view rbAddTapGesWithTarget:self action:@selector(paramsType2ViewTapped:)];
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.name];
            [view addPriority:1];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(21) bottom:Zoom(21) left:Zoom(16) right:0 centerY:0];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textLightBlackColor] align:2 text:@""];
            view.tag = 333;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:0 centerY:0];
        
        [view h_view_addView:[RanTool imgViewWithMode:1 radius:0 imgName:@"mine_arrow_right"] withHeight:Zoom(13) width:Zoom(8) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(12) right:Zoom(11) centerY:YES];

        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:Zoom(12) left:Zoom(15) right:Zoom(15) centerX:0];
    
    [self reloadParamsType2:view];
    return view;
}
/** 3选择器模板[弹出] */
- (UIView *)paramsSelectorPopType3ViewWithData: (RBAddOrderSpecModel *)data
{
    UIView *view = UIView.new;
    view.tag = 2003;
    view.rbExData = data;
    [view rbAddTapGesWithTarget:self action:@selector(paramsType3ViewTapped:)];
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.name];
            [view addPriority:1];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(21) bottom:Zoom(21) left:Zoom(16) right:0 centerY:0];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textLightBlackColor] align:2 text:@""];
            view.tag = 333;
            view.tag = 333;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:0 centerY:0];
        
        [view h_view_addView:[RanTool imgViewWithMode:1 radius:0 imgName:@"mine_arrow_right"] withHeight:Zoom(13) width:Zoom(8) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(12) right:Zoom(11) centerY:YES];

        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:Zoom(12) left:Zoom(15) right:Zoom(15) centerX:0];
    
    return view;
}
/** 4单选模板[弹出][多项] */
- (UIView *)paramsSinglePopType4ViewWithData: (RBAddOrderSpecModel *)data
{
    UIView *view = UIView.new;
    view.tag = 2004;
    view.rbExData = data;
    [view rbAddTapGesWithTarget:self action:@selector(paramsType4ViewTapped:)];
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.name];
            [view addPriority:1];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(21) bottom:Zoom(21) left:Zoom(16) right:0 centerY:0];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textLightBlackColor] align:2 text:@""];
            view.tag = 333;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:0 centerY:0];
        
        [view h_view_addView:[RanTool imgViewWithMode:1 radius:0 imgName:@"mine_arrow_right"] withHeight:Zoom(13) width:Zoom(8) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(12) right:Zoom(11) centerY:YES];

        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:Zoom(12) left:Zoom(15) right:Zoom(15) centerX:0];
    
    return view;
}
/** 5牙位模板[显示]; */
- (UIView *)paramsChooseToothDisplayType5ViewWithData: (RBAddOrderSpecModel *)data
{
    UIView *view = UIView.new;
    view.rbExData = data;
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        //牙齿选择
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@"牙齿选择"];
                [view addPriority:1];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.is_add_type == 0? @"(固定价格)": @"(商品价格)"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            view;
        }) withHeight:Zoom(44) width:CGFLOAT_MIN top:0 bottom:Zoom(10) left:Zoom(16) right:Zoom(16) centerX:0];
        //牙齿
        NSArray *toothArray = [self toothArrayWithModel: data];
        for (NSDictionary *dic in toothArray)
        {
            [view view_addView:[self toothViewWithData:dic index:[toothArray indexOfObject:dic]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(16) bottom:Zoom(26) left:0 right:0 centerX:0];
        }
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:Zoom(12) left:Zoom(15) right:Zoom(15) centerX:NO];
    return view;
}
/** 6牙排模板[弹出] */
- (UIView *)paramsAddToothPopType6ViewWithData: (RBAddOrderSpecModel *)data
{
    UIView *view = UIView.new;
    view.tag = 2006;
    view.rbExData = data;
    [view rbAddTapGesWithTarget:self action:@selector(paramsType6ViewTapped:)];
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.name];
            [view addPriority:1];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(21) bottom:Zoom(21) left:Zoom(16) right:0 centerY:0];
        
        [view h_view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(11) weight:0 color:[RBThemeConfig textLightBlackColor] align:2 text:@""];
            view.tag = 333;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:0 centerY:0];
        
        [view h_view_addView:[RanTool imgViewWithMode:1 radius:0 imgName:@"mine_arrow_right"] withHeight:Zoom(13) width:Zoom(8) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(12) right:Zoom(11) centerY:YES];

        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:Zoom(12) left:Zoom(15) right:Zoom(15) centerX:0];
    
    return view;
}

- (NSArray *)toothArrayWithModel: (RBAddOrderSpecModel *)model
{
    NSMutableArray *arrayM = NSMutableArray.array;
    for (NSInteger i = 0; i < 4; i++)
    {
        NSMutableDictionary *dic = NSMutableDictionary.dictionary;
        dic[@"title"] = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (RBAddOrderSpecItemModel *item in model.spec)
            {
                [arrayM addObject:item.spec_name];
            }
            arrayM;
        })[i];
        dic[@"img"] = @[@"tup",@"tup",@"tdown",@"tdown"][i];
        dic[@"imgSel"] = @[@"tupSel",@"tupSel",@"tdownSel",@"tdownSel"][i];
        dic[@"count"] = @"8";
        dic[@"data"] = model.spec[i];
        [arrayM addObject:dic];
    }
    return arrayM;
}




- (NSArray *)toothArray
{
    if (!_toothArray)
    {
        self.toothArray = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (NSInteger i = 0; i < 4; i++)
            {
                NSMutableDictionary *dic = NSMutableDictionary.dictionary;
                dic[@"title"] = @[@"上颌右侧",@"上颌左侧",@"下颌右侧",@"下颌左侧"][i];
                dic[@"img"] = @[@"tup",@"tup",@"tdown",@"tdown"][i];
                dic[@"imgSel"] = @[@"tupSel",@"tupSel",@"tdownSel",@"tdownSel"][i];
                dic[@"count"] = @"8";
                [arrayM addObject:dic];
            }
            arrayM;
        });
    }
    return _toothArray;
}

- (NSMutableArray *)toothBtnsArrayM
{
    if (!_toothBtnsArrayM)
    {
        self.toothBtnsArrayM = NSMutableArray.array;
    }
    return _toothBtnsArrayM;
}

- (UIView *)toothViewWithData: (NSDictionary *)dic index:(NSInteger)index
{
    UIView *view = UIView.new;
    [view view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:dic[@"title"]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:0 centerX:0];
    [view view_addView:({
        UIView *view = UIView.new;
        NSInteger count = [dic[@"count"] integerValue];
        for (NSInteger i = 1; i <= count; i++)
        {
            UIButton *btn = UIButton.new;
            if ([dic[@"img"] containsString:@"tup"])
            {
                btn.titleEdgeInsets = UIEdgeInsetsMake(Zoom(10), 0, 0, 0);
            }
            else
            {
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, Zoom(5), 0);
            }
            [btn setBackgroundImage:[UIImage imageNamed:dic[@"img"]] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:dic[@"imgSel"]] forState:UIControlStateSelected];
            [btn rbSetTitleColor:UIColor.whiteColor];
            [btn rbSetFontSize:Zoom(13)];
            btn.tag = 20 * index + i;
            [btn rbSetTitle:[NSString stringWithFormat:@"%zd", i]];
            [btn rbAddTarget:self action:@selector(toothBtnTapped:)];
            [self.toothBtnsArrayM addObject:btn];
            [view h_view_addView:btn withHeight:CGFLOAT_MIN width:Zoom(24) top:0 bottom:0 left:i == 1? 0: Zoom(18) right:CGFLOAT_MIN centerY:0];
        }
        view;
    }) withHeight:Zoom(27) width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(13) right:0 centerX:0];
    return view;
}

@end
